'''
Created on 17.01.2014

@author: christine
'''

import os
from io import open
import trunk.data
import glob

def compare_lemmatization():
    """Compares the lemmatization output of the lemmas that the wordform is being compared to when the length
    of the lemmas are constrained versus when it is not."""
    
    same = 0
    different = 0
    differentdict = dict()
    mypath = os.path.join(trunk.data.__path__[0], "usp", "Evaluation_corpus")
    os.chdir(mypath)

    old = "Lemmatization" + "-1" + ".txt"
    new = "Lemmatization" + "-2" + ".txt"
    with open(old, encoding = "utf-8") as o:
        oldmappings = o.readlines()
        with open(new, encoding = "utf-8") as n:
            i = 0
            newmappings = n.readlines()
            while(i < len(oldmappings)):
                om = oldmappings[i]
                nm = newmappings[i]
                if om == nm:
                    #print("Gleich:", nm, om)
                    same += 1
                else:
                    #print("Unterschiedlich:", nm, om)
                    different += 1
                    differentdict[om] = nm
                i +=1
    print("In total there are " + str(same) + " same and " + str(different) + " different lemmatizations between the two functions")
    return differentdict

def evaluate():
    """
    function for evaluation the lemmatizer
    """
    mypath = os.path.join(trunk.data.__path__[0], "usp", "Evaluation_corpus")
    os.chdir(mypath)
    correct = 0
    all = 0
    sentence = 1
    
    with open("Evaluation.txt", encoding = "utf-8") as f:
        gold = ""
        lemmatizer = ""
        for line in f:
            if line.startswith("\\z"):
                sentence += 1
                lemmatizer = line[2:].strip().split()
            if line.startswith("\\S"):
                gold = line[2:].strip().split()
            if lemmatizer != "" and gold != "":
            
                if len(lemmatizer) != len(gold):
                    print "Sentence differ in length.", sentence
                    print gold
                    
                else:  
                    i = 0
                    while i < len(gold):
                        if gold[i].split("/")[0] != "???":
                            all += 1
                            if gold[i].split("/")[0] == lemmatizer[i].split("/")[0]:
                                correct += 1
                            else:
                                print "\nSentence:", sentence
                                print "Gold:", gold
                                print "Lemm:", lemmatizer
                                print gold[i], "-->", lemmatizer[i]
                        i+=1
                gold = ""
                lemmatizer = ""
            
        print "\n\ncorrect:", correct
        print "all:", all    
                

if __name__ == '__main__':
    evaluate()
    #differentdict = compare_lemmatization()
    #for (key, value) in differentdict.items():
     #   print("Lemmatisierung ohne Constraint\t" + key)
      #  print("Lemmatisierung mit Constraint\t" + value)